package ihm;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import appli.Launcher;
import appli.utils.Config;
import appli.utils.MoveEnum;
import appli.utils.SmaConstante;

/**
 * @author Arnaud
 * 
 */
public class ConfigSmaPanel extends JPanel {
	
	private static final long serialVersionUID = 8494872063465732463L;
	
	private AppliWindow frame;
	
	private GridBagConstraints c;
	
	/**
	 * 
	 */
	public JTextField textDeltaT;
	/**
	 * 
	 */
	public JTextField textAvoidDecisive;
	/**
	 * 
	 */
	public JTextField textGoalAttraction;
	/**
	 * 
	 */
	public JTextField textAvoidObstacle;
	/**
	 * 
	 */
	public JTextField textK;
	
	/**
	 * 
	 */
	public JTextField textDeltaTPark;
	/**
	 * 
	 */
	public JTextField textAvoidDecisivePark;
	/**
	 * 
	 */
	public JTextField textGoalAttractionPark;
	/**
	 * 
	 */
	public JTextField textAvoidObstaclePark;
	/**
	 * 
	 */
	public JTextField textKPark;

	/**
	 * Constructeur.
	 * @param _frame La frame dans laquelle el panel apparait
	 */
	public ConfigSmaPanel(AppliWindow _frame){
		
		frame = _frame;
		
		this.setLayout(new GridBagLayout());
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(5,5,5,5);  //padding
        
		 //Create the two buttons
        JButton startButton = new JButton("Start Demo");
        
        //Create the action listener for button
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                //startAction();
            }

        });
        
        c.gridx = 0;
    	c.gridy = 0;
        this.add(new JLabel("Constantes"),c);
       
        c.gridx = 1;
        this.add(new JLabel("Normal"),c);
        
        c.gridx = 2;
        this.add(new JLabel("Park"),c);
        
        c.gridx = 0;
    	c.gridy = 1;
        this.add(new JLabel("Decisive Agent"),c);
        
        
        c.gridx = 0;
    	c.gridy = 2;
        this.add(new JLabel("Avoids decisives"),c);
        
        textAvoidDecisive = new JTextField(""+Config.SMA_CONSTANTE.avoidsDecisive); 
        c.gridx = 1;
        this.add(textAvoidDecisive,c);
        
        textAvoidDecisivePark = new JTextField(""+Config.SMA_CONSTANTE_PARK.avoidsDecisive); 
        c.gridx = 2;
        this.add(textAvoidDecisivePark,c);
        
    	c.gridx = 0;
    	c.gridy = 3;       
        this.add(new JLabel("Goal attraction"),c);
        
        c.gridx = 1;
        textGoalAttraction = new JTextField(""+Config.SMA_CONSTANTE.goalAttraction); 
        this.add(textGoalAttraction,c);
        
        c.gridx = 2;
        textGoalAttractionPark = new JTextField(""+Config.SMA_CONSTANTE_PARK.goalAttraction); 
        this.add(textGoalAttractionPark,c);
        
        c.gridx = 0;
    	c.gridy = 4; 
        this.add(new JLabel("Avoids obstacles"),c);
        
        c.gridx = 1;
        textAvoidObstacle = new JTextField(""+Config.SMA_CONSTANTE.avoidsObstacle); 
        this.add(textAvoidObstacle,c);
        
        c.gridx = 2;
        textAvoidObstaclePark = new JTextField(""+Config.SMA_CONSTANTE_PARK.avoidsObstacle); 
        this.add(textAvoidObstaclePark,c);
        
        c.gridx = 0;
    	c.gridy = 5;
        this.add(new JLabel("Delta T"),c);
        
        c.gridx = 1;
        textDeltaT = new JTextField(""+Config.SMA_CONSTANTE.deltaT);
        this.add(textDeltaT,c);
        
        c.gridx = 2;
        textDeltaTPark = new JTextField(""+Config.SMA_CONSTANTE_PARK.deltaT);
        this.add(textDeltaTPark,c);
        
        c.gridx = 0;
    	c.gridy = 6;
    	this.add(new JLabel("MassCenter :"),c);
              
		c.gridx = 0;
		c.gridy = 7;
		this.add(new JLabel("K :"),c);
		 
		c.gridx = 1;
		textK = new JTextField(""+Config.SMA_CONSTANTE.k);
		this.add(textK,c);
		
		c.gridx = 2;
		textKPark = new JTextField(""+Config.SMA_CONSTANTE_PARK.k);
		this.add(textKPark,c);
		     	
    	this.setVisible(true);
    	
    	this.validate();
    	this.repaint();
       
	}
	
	
	/**
	 * Enabled or disabled textinputfield depending of the parkmode
	 */
	public void switchToParkMode(){
		
		if(Config.PARK_MODE){
			this.textAvoidDecisivePark.setEnabled(false);
			this.textAvoidObstaclePark.setEnabled(false);
			this.textDeltaTPark.setEnabled(false);
			this.textGoalAttractionPark.setEnabled(false);
			this.textKPark.setEnabled(false);
			
			this.textAvoidDecisive.setEnabled(true);
			this.textAvoidObstacle.setEnabled(true);
			this.textDeltaT.setEnabled(true);
			this.textGoalAttraction.setEnabled(true);
			this.textK.setEnabled(true);
		}else{
			
			this.textAvoidDecisivePark.setEnabled(true);
			this.textAvoidObstaclePark.setEnabled(true);
			this.textDeltaTPark.setEnabled(true);
			this.textGoalAttractionPark.setEnabled(true);
			this.textKPark.setEnabled(true);
			
			this.textAvoidDecisive.setEnabled(false);
			this.textAvoidObstacle.setEnabled(false);
			this.textDeltaT.setEnabled(false);
			this.textGoalAttraction.setEnabled(false);
			this.textK.setEnabled(false);
		}
	}
	
	/**
	 * @param parkMode
	 * @return Sma Config
	 */
	public SmaConstante getConstantes(boolean parkMode){
		
		SmaConstante smaConstante = new SmaConstante();
		
		if(parkMode){
						
			smaConstante.avoidsObstacle = Double.parseDouble(this.textAvoidObstaclePark.getText());
			smaConstante.deltaT = Double.parseDouble(this.textDeltaTPark.getText());
			smaConstante.goalAttraction = Double.parseDouble(this.textGoalAttractionPark.getText());
			smaConstante.k = Double.parseDouble(this.textKPark.getText());
			smaConstante.avoidsDecisive = Double.parseDouble(this.textAvoidDecisivePark.getText());
			
		}else{
			smaConstante.avoidsObstacle = Double.parseDouble(this.textAvoidObstacle.getText());
			smaConstante.deltaT = Double.parseDouble(this.textDeltaT.getText());
			smaConstante.goalAttraction = Double.parseDouble(this.textGoalAttraction.getText());
			smaConstante.k = Double.parseDouble(this.textKPark.getText());
			smaConstante.avoidsDecisive = Double.parseDouble(this.textAvoidDecisive.getText());	
		}
		
		return smaConstante;
	}


	
}
